package proj1.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Set;
import java.util.TreeSet;

import proj1.constants.C.COMPARATOR;
import proj1.oracle.OracleDB;
import proj1.util.Logger;

// Build these objects from the 
// values found in a particular vendor's inventory.
// Tables RDIMARS, RDIMBEH, RDIMDAN, RDIMMOD
public class Radiator {
	private static Logger log = Logger.getLogger(Radiator.class);
	
	private String partNumber;
	private String core;
	private String inHead;
	private String outHead;
	private String inCon;
	private String outCon;
	private String tMount;
	private String oilCool;
	private Double price;
	private Long quantity; // amount
	
	public enum SIZE { STANDARD("Standard", "1"), HEAVY("Heavy", "2"), AIR_CONDITION("Air Condition", "3"), MAXIMUM("Maximum", "4"); 
		String colNum;
		String displayName;
		
		private SIZE (String displayName, String colNum){
			this.displayName = displayName;
			this.colNum = colNum;
		}
		
		@Override
		public String toString(){
			return displayName;
		}
	}
	
	public static Set<Radiator> radiatorsFromResults(ResultSet rs) throws SQLException{
		Set<Radiator> result = new TreeSet<Radiator>(COMPARATOR.RADIATOR);
		while(rs.next())
			result.add(
				new Radiator(
					rs.getString("P_NUMBER"),
					rs.getString("CORE"),
					rs.getString("INHEAD"),
					rs.getString("OUTHEAD"),
					rs.getString("INCON"),
					rs.getString("OUCON"),
					rs.getString("TMOUNT"),
					rs.getString("OILCOOL"),
					rs.getDouble("PRICE"), 
					rs.getLong("AMOUNT")
					)
			);
		return result;
	}
	
	public Radiator(String partNumber, String core, String inHead, String outHead,
			String inCon, String outCon, String tMount, String oilCool, Double price, Long quantity){
		this.partNumber = partNumber;
		this.core = core;
		this.inHead = inHead;
		this.outHead = outHead;
		this.inCon = inCon;
		this.outCon = outCon;
		this.tMount = tMount;
		this.oilCool = oilCool;
		this.price = price;
		this.quantity = quantity;
	}
	
	public String getPartNumber(){ return partNumber; }
	public String getCore(){ return core; }
	public String getInHead(){ return inHead; }
	public String getOutHead(){ return outHead; }
	public String getInCon(){ return inCon; }
	public String getOutCon(){ return outCon; }
	public String getTMount(){ return tMount; }
	public String getOilCool(){ return oilCool; }
	public Double getPrice(){ return price; }
	public Long getQuantity(){ return quantity; }
	
	@Override
	public String toString(){
		return "Part Number: " + partNumber;
	}

	public boolean save( Model model, SIZE size, Vendor vendor) {
		String rLink = String.valueOf(model.getRLink());
		String sizeCol = size.colNum;
		OracleDB db = OracleDB.getInstance();
		
		// Insert into the vendor parts table
		String sql1 = "INSERT INTO RDIM" + vendor + " (P_NUMBER, CORE, INHEAD, OUTHEAD, INCON, OUCON, TMOUNT, OILCOOL, PRICE, AMOUNT) values ( '" +
				partNumber + "', '" + core + "', '" + inHead + "', '" + outHead + "', '" + inCon + "', '" + outCon + "', '" + tMount + "', '" + oilCool + "', " + price + ", " + quantity + ") ";
		
		boolean success = 1 == db.executeUpdate(sql1);

		if(success){
			String sql2 = "UPDATE RADCRX SET " + vendor + sizeCol + " = " + partNumber + " WHERE RLINK=" + rLink;  
			success = 1 == db.executeUpdate(sql2);
		}
		
		return success;
	}
	
	
    
	//Deletion is pretty darn similar to saving
	public boolean delete(Model model, SIZE size, Vendor vendor){
		String rLink = String.valueOf(model.getRLink());
		String sizeCol = size.colNum;
		boolean passed = true;
		OracleDB db = OracleDB.getInstance();
		
		String sql;// = "DELETE FROM RDIM" + vendor + " WHERE P_NUMBER = '" + partNumber + "'";
		
		//if(db.executeUpdate(sql) == 1){
		
			sql = "UPDATE RADCRX SET " + vendor + sizeCol + " = NULL WHERE RLINK = " + '\'' + rLink + '\'';
			if(db.executeUpdate(sql) == 1){
				
				db.executeQuery("COMMIT");
			} else {
				db.executeQuery("rollback");
				passed = false;
			}
		//} else {
			//db.executeQuery("rollback");
			//passed = false;
		//}
		
		return passed;
	}
		
	
	// The long form update.
	public boolean update(Model model, SIZE size, Vendor vendor, String newPartNumber, String newCore,
			String newInHead, String newOutHead, String newInCon, String newOutCon, String newTMount, String newOilCool,
			Double newPrice, Long newQuantity){
		String rLink = String.valueOf(model.getRLink());
		String sizeCol = size.colNum;
		boolean passed = true;
		OracleDB db = OracleDB.getInstance();
		
		String sql = "UPDATE RDIM" + vendor + " SET P_NUMBER = '" + newPartNumber + "', CORE = '" + newCore + "', INHEAD = '" + newInHead +
				"', OUTHEAD = '" + newOutHead + "', INCON = '" + newInCon + "', OUCON = '" + newOutCon + "', TMOUNT = '" + newTMount +
				"', OILCOOL = '" + newOilCool + "', PRICE = '" + newPrice + "', AMOUNT = '" + newQuantity + "' WHERE P_NUMBER = '" +
				partNumber + '\''; //updates the old partnumber with new part number if applicable
		
		if(db.executeUpdate(sql)==1){
			
			sql = "UPDATE RADCRX SET " + vendor + sizeCol + " = " + newPartNumber + " WHERE RLINK = " + rLink;
			if(db.executeUpdate(sql) == 1){
				
				db.executeQuery("COMMIT");
			} else {
				db.executeQuery("rollback");
				passed = false;
			}
		} else {
				db.executeQuery("rollback");
				passed = false;
		}
		
		return passed;
		
	}
	
	//for quick decrementing/incrementing
	public boolean update(Model model, SIZE size, Vendor vendor, Long newQuantity){
		
		return update(model, size, vendor, partNumber, core, inHead, outHead, inCon, outCon, tMount, oilCool, price, newQuantity);
		
	}
}
